import json

import requests

from global_config import root

imagine_url = "http://10.31.120.162:7861/"


def img2img(prompts, imgurl):
    with open(f'{root}\scripts\midjourney\data.json', 'r',
              encoding='utf-8') as file:
        dir_api_body = json.load(file)
        dir_api_body['data']['options'][0]['value'] = f"{prompts} {imgurl}"
        headers = {
            "Content-Type": "application/json",
            'Cookie': '__dcfduid=b3412fe0f2f011ed9bfadde945ac52ed; __sdcfduid=b3412fe1f2f011ed9bfadde945ac52ed781318836c960d6008c5a8c23f2c0498590188f04dc6e8f78ddd5cd1a3497ce5; _ga=GA1.1.1098397453.1687261352; OptanonConsent=isIABGlobal=false&datestamp=Tue+Jun+20+2023+19%3A42%3A33+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=6.33.0&hosts=&landingPath=NotLandingPage&groups=C0001%3A1%2CC0002%3A1%2CC0003%3A1&AwaitingReconsent=false; _ga_Q149DFWHT7=GS1.1.1687261351.1.1.1687261356.0.0.0; cf_clearance=pNYSRQTngMW8MKOyWA3_rIO71Tb3_dYHfPmuAdcOT0s-1713327476-1.0.1.1-10S.Xup7B28_N5beFQCtvEQHsGmvEVHoV9KuCwRuuI4QTb0tX.565h2yuaB9kOXfVp.dYZDIXQ6dRQNKrBfBTg; dbind=6834790a-5b47-4863-b2e1-40f28d0f16e2; cf_clearance=89uAK3vV1WM.cd5jH9c2FO_nOcjQWtq2bwgcn4D0zFA-1716191212-1.0.1.1-mXkMRQ.OEO9wu6z3BVZSRd_6jAyLGCM6Xo5lbprScQbQXdrLUo5M4DFpH78XloKXcakUMHtaCCQYV2lnXQifgg; __cfruid=8a146aa4fdb6857260a47c60a9237180432f113d-1716196222; _cfuvid=bwsp2P74Psw2vqJFnzwl7tmxGWXA2OPfSuUvOAeomvQ-1716196222931-0.0.1.1-604800000',
            'Authorization': 'MTIxOTg0OTAyMDg2OTA1NDQ5Ng.GcBauI.sftVnZCqdO7CrJ7Wzie8yuSf1QJSvfJNSLcY7s'
        }

        data = {"prompt": prompts,
                "picurl": imgurl}

        response = requests.post(imagine_url, headers=headers, json=data)

        print(response.status_code, response.text)
        if response.status_code == 200:
            resp = response.json()
            return resp['trigger_id']

